package com.example.question.tree.tree1;

/**
 * @Date 2023-07-09
 * @Author Jonathan
 */
public class Code07_543 {
    public static void main(String[] args) {

    }

    public int diameterOfBinaryTree(TreeNode root) {
        return process(root).maxDistance;
    }

    private Info process(TreeNode root) {
        if (root == null) {
            return new Info(0, 0);
        }
        Info left = process(root.left);
        Info right = process(root.right);

        int maxDepth = Math.max(left.maxDepth, right.maxDepth) + 1;
        // 不经过的话就是左边的最大距离和右边的最大距离去最大值
        int maxDistance = Math.max(left.maxDistance, right.maxDistance);
        // 经过当前节点的话 就是左边的深度加上右边的深度
        maxDistance = Math.max(maxDistance, left.maxDepth + right.maxDepth);
        return new Info(maxDepth, maxDistance);

    }

    static class Info {
        private int maxDepth;
        private int maxDistance;

        public Info(int maxDepth, int maxDistance) {
            this.maxDepth = maxDepth;
            this.maxDistance = maxDistance;
        }
    }
}
